import os
import shutil

def split_multi_dirs(input_dir, output_dir):
    for folder in os.listdir(input_dir):
        folder_path = os.path.join(input_dir, folder)

        if os.path.isdir(folder_path):
            for subfolder in os.listdir(folder_path):
                subfolder_path = os.path.join(folder_path, subfolder)

                if os.path.isdir(subfolder_path):
                    #判断是否为空
                    if os.listdir(subfolder_path):
                        #构造输出路径
                        new_path = os.path.join(output_dir, subfolder)

                        if os.path.exists(new_path):
                            print(f"{new_path} exists")
                            exit(-1)

                        shutil.move(subfolder_path, new_path)
                        print(f"moved : {subfolder_path} -> {new_path}")
                    else:
                        print(f"skipped empty folder: {subfolder_path}")

if __name__ == "__main__":
    input_dir = r"/media/imed/d9f520b2-6e7a-4842-af77-fba3ad2d8d27/12222"   # 顶层文件夹
    output_dir = r"/media/imed/d9f520b2-6e7a-4842-af77-fba3ad2d8d27/12222_to" # 输出文件夹
    os.makedirs(output_dir, exist_ok=True)


    split_multi_dirs(input_dir, output_dir)